public class LoanCalculator {
    public double calculateTotalInterest(double principal, int months, double annualInterestRate, boolean isFixedPrincipal) {
        double monthlyInterestRate = annualInterestRate / 12 / 100;
        double totalInterest = 0;
        if (isFixedPrincipal) {
            for (int i = 1; i <= months; i++) {
                totalInterest += (principal - (principal / months) * (i - 1)) * monthlyInterestRate;
            }
        } else { // 等额本息
            double monthlyPayment = (principal * monthlyInterestRate * Math.pow(1 + monthlyInterestRate, months)) / (Math.pow(1 + monthlyInterestRate, months) - 1);
            for (int i = 1; i <= months; i++) {
                totalInterest += monthlyPayment > principal ? (principal * monthlyInterestRate) : monthlyPayment;
                principal -= monthlyPayment - (principal * monthlyInterestRate);
            }
        }
        return totalInterest;
    }
}